﻿using Common.Contract;
using Common.Contract.Authentication;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Contract.Customer;
using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request.CustomerManager;
using Common.Contract.Customer.Response.CustomerManager;
using Common.Service;
using SMECustomerWebsite.Controllers.core;
using SMECustomerWebsite.Core.Configuration;
using SMECustomerWebsite.Core.Resources;
using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Web.Mvc;

namespace SMECustomerWebsite.Controllers
{
    public class SystemController : ControllerBase
    {
        #region Menu & Header
        //[Authorize]
        [ChildActionOnly]
        public ActionResult HeaderMenu()
        {
            return PartialView("_HeaderMenu", this.CurrentUserRecord);
        }

        //[Authorize]
        [ChildActionOnly]
        public ActionResult Sidebar()
        {
            UserRecord userRecord = this.CurrentUserRecord;
            userRecord.MenuNodes = this.MenuNodes;
            return PartialView("_Sidebar", userRecord);
        }

        [ChildActionOnly]
        public ActionResult BusinessDateInfo(string name)
        {
            string date = string.Empty;
            DateTime? businessDate = GetBusinessDateByTableName(name);
            date = businessDate.HasValue ? businessDate.Value.ToShortDateString() : string.Empty;
            return PartialView("BusinessDateInfo", date);
        }

        [ChildActionOnly]
        public ActionResult PopUpAlert()
        {
            List<CountAlertCustomerRecord> resultCustomer = new List<CountAlertCustomerRecord>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetriveCountCustomerAlertRequest request = new RetriveCountCustomerAlertRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerI2BDetailData"
                    },
                    CurrentUser = this.CurrentUserRecord,
                };
                var customerRecords = new RetrieveCountCustomerAlertResponse();
                customerRecords = serviceClient.Proxy.RetriveCountCustomerAlert(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CountAlertCustomerRecord>() : customerRecords.Records;
            }
            resultCustomer = MapNameAndDescriptionAlertRecord(resultCustomer);
            return PartialView("PopUpAlert", resultCustomer);
        }

        [ChildActionOnly]
        public ActionResult AlertHeader()
        {
            List<CountAlertCustomerRecord> resultCustomer = new List<CountAlertCustomerRecord>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                RetriveCountCustomerAlertRequest request = new RetriveCountCustomerAlertRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerI2BDetailData"
                    },
                    CurrentUser = this.CurrentUserRecord,
                };
                var customerRecords = new RetrieveCountCustomerAlertResponse();
                customerRecords = serviceClient.Proxy.RetriveCountCustomerAlert(request);
                resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CountAlertCustomerRecord>() : customerRecords.Records;
            }
            resultCustomer = MapNameAndDescriptionAlertRecord(resultCustomer);
            return PartialView("AlertHeader", resultCustomer);
        }

        [ChildActionOnly]
        public ActionResult SubMenuAdminChecklistDetail()
        {
            return PartialView("_SubMenuAdminChecklistDetail");
        }

        [ChildActionOnly]
        public ActionResult SubMenuCustomer()
        {
            return PartialView("_SubMenuCustomer");
        }
        [ChildActionOnly]
        public ActionResult SubMenuXSelling()
        {
            return PartialView("_SubMenuXSelling");
        }
        [ChildActionOnly]
        public ActionResult SubMenuSalePerformance()
        {
            return PartialView("_SubMenuSalePerformance");
        }

        [ChildActionOnly]
        public ActionResult SubMenuLoanSalePerformance()
        {
            return PartialView("_SubMenuLoanSalePerformance");
        }
        [ChildActionOnly]
        public ActionResult SubMenuPortfolioManagement()
        {
            return PartialView("_SubMenuPortfolioManagement");
        }

        [ChildActionOnly]
        public ActionResult SubMenuAlertCust()
        {
            return PartialView("_SubMenuAlertCust");
        }
        [ChildActionOnly]
        public ActionResult SubMenuAlertBadbank()
        {
            return PartialView("_SubMenuAlertBadbank");
        }
        [ChildActionOnly]
        public ActionResult SubMenuAlertRisk()
        {
            return PartialView("_SubMenuAlertRisk");
        }
        [ChildActionOnly]
        public ActionResult SubMenuCelebration()
        {
            return PartialView("_SubMenuCelebration");
        }

        [ChildActionOnly]
        public ActionResult SubMenuKpi()
        {
            return PartialView("_SubMenuKpi", CurrentUserRecord);
        }

        [ChildActionOnly]
        public ActionResult SubMenuKpiRawData()
        {
            return PartialView("_SubMenuKpiRawData");
        }
        [ChildActionOnly]
        public ActionResult SubMenuSaleContestRanking()
        {
            return PartialView("_SubMenuSaleContestRanking", CurrentUserRecord);
        }
        [ChildActionOnly]
        public ActionResult SubMenuSaleContestRawData()
        {
            return PartialView("_SubMenuSaleContestRawData", CurrentUserRecord);
        }
        [ChildActionOnly]
        public ActionResult SubMenuAlertMaturity()
        {
            return PartialView("_SubMenuAlertMaturity", CurrentUserRecord);
        }
        [ChildActionOnly]
        public ActionResult SubMenuSaleContestRankingBack()
        {
            return PartialView("_SubMenuSaleContestRankingBack");
        }
        [ChildActionOnly]
        public ActionResult SubMenuPreApprovedCreditCard()
        {
            return PartialView("_SubMenuPreApprovedCreditCard");
        }
        #endregion
        #region Show Info
        [ChildActionOnly]
        public ActionResult CustInfo()
        {
            return PartialView("_CustInfo");
        }
        [ChildActionOnly]
        public ActionResult FeedbackInfo()
        {
            return PartialView("_FeedbackInfo");
        }
        [ChildActionOnly]
        public ActionResult InfoNeedUpdateInfo()
        {
            return PartialView("_InfoNeedUpdateInfo");
        }
        [ChildActionOnly]
        public ActionResult CustomerHuntingInfo()
        {
            return PartialView("_CustomerHuntingInfo");
        }
        #endregion
        #region Table Temp
        [ChildActionOnly]
        public ActionResult CustByProductDepositTableTemp()
        {
            return PartialView("_CustByProductDepositTableTemp", this.CurrentUserRecord);
        }
        [ChildActionOnly]
        public ActionResult CustByProductLoanTableTemp()
        {
            return PartialView("_CustByProductLoanTableTemp", this.CurrentUserRecord);
        }
        [ChildActionOnly]
        public ActionResult CustByProductPosTableTemp()
        {
            return PartialView("_CustByProductPosTableTemp", this.CurrentUserRecord);
        }
        [ChildActionOnly]
        public ActionResult CustByProductCardTableTemp()
        {
            return PartialView("_CustByProductCardTableTemp", this.CurrentUserRecord);
        }
        [ChildActionOnly]
        public ActionResult CustByProductI2BTableTemp()
        {
            return PartialView("_CustByProductI2BTableTemp", this.CurrentUserRecord);
        }
        [ChildActionOnly]
        public ActionResult CustByProductTradeTableTemp()
        {
            return PartialView("_CustByProductTradeTableTemp", this.CurrentUserRecord);
        }
        [ChildActionOnly]
        public ActionResult CustByProductChuoiTableTemp()
        {
            return PartialView("_CustByProductChuoiTableTemp", this.CurrentUserRecord);
        }

        [ChildActionOnly]
        public ActionResult CustSubSegmentMSMETemp()
        {
            return PartialView("_CustSubSegmentMSMETemp", this.CurrentUserRecord);
        }
        [ChildActionOnly]
        public ActionResult CustSubSegmentMMETemp()
        {
            return PartialView("_CustSubSegmentMMETemp", this.CurrentUserRecord);
        }
        [ChildActionOnly]
        public ActionResult CustSubSegmentSMETemp()
        {
            return PartialView("_CustSubSegmentSMETemp", this.CurrentUserRecord);
        }
        #endregion
        #region Multi Lang
        public ActionResult ChangeLanguage(string lang)
        {
            new SiteLanguages().SetLanguage(lang);
            return Redirect(Request.UrlReferrer.ToString());
        }
        #endregion
        #region Send Q&A
        [ValidateInput(false)]
        [Authorize]
        [HttpPost]
        public ActionResult SendQA(string data)
        {
            try
            {
                if (!string.IsNullOrEmpty(data))
                {
                    //MailMessage mail = new MailMessage();
                    //mail.To.Add("tudd1@vpbank.com.vn");
                    //mail.From = new MailAddress("tudd1@vpbank.com.vn");
                    //mail.Subject = "Question from " + CurrentUserRecord.UserName;
                    //mail.Body = data;
                    //mail.IsBodyHtml = true;
                    //SmtpClient smtp = new SmtpClient();
                    //smtp.Host = "smtp.office365.com";
                    //smtp.Port = 25;
                    //smtp.UseDefaultCredentials = false;
                    //smtp.Credentials = new System.Net.NetworkCredential
                    //("tudd1@vpbank.com.vn", "Abc@1234");// Enter seders User name and password
                    //smtp.EnableSsl = true;
                    //smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    //smtp.Send(mail);

                    using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
                    {
                        UserQuestionRecord record = new UserQuestionRecord()
                        {
                            Title = data,
                            Username = CurrentUserRecord.UserName
                        };
                        UpdateUserQuestionRequest request = new UpdateUserQuestionRequest
                        {
                            Header = new RequestHeaderRecord
                            {
                                ApplicationKey = SystemConfiguration.ApplicationKey,
                                CallerName = "SMEsCustomer360Degree.SaveLog"
                            },
                            Record = record
                        };
                        var response = serviceClient.Proxy.UpdateUserQuestion(request);

                    }
                }
                return Json(true);
            }
            catch (System.Exception e)
            {
                return Json(e.Message);
            }
        }
        #endregion
        #region Helper methods

        private List<CountAlertCustomerRecord> MapNameAndDescriptionAlertRecord(List<CountAlertCustomerRecord> listRecords)
        {
            foreach (var item in listRecords)
            {
                if (item.CountCust > 0)
                {
                    switch (item.AlertTitle)
                    {
                        case "Badbank":
                            item.AlertDescription = Resource.CustOverdueTitleDesc;
                            item.AlertName = Resource.CustOverdue;
                            item.IsDaily = true;
                            item.AlertUrl = Url.Action("AlertOverdueDate", "CustomerAlert");
                            item.AlertCss = "fa fa-ban";
                            break;
                        case "Loan":
                            item.AlertDescription = Resource.AlertLoanMatdtDesc;
                            item.AlertName = Resource.PopupAlertLoanMaturity;
                            item.IsDaily = true;
                            item.AlertUrl = Url.Action("AlertLoanMaturity", "CustomerAlert");
                            item.AlertCss = "fa fa-exclamation-circle";
                            break;
                        case "Deposit":
                            item.AlertDescription = Resource.AlertDepositMaturityDesc;
                            item.AlertName = Resource.PopupAlertDepositMaturity;
                            item.IsDaily = true;
                            item.AlertUrl = Url.Action("AlertDepositMaturity", "CustomerAlert");
                            item.AlertCss = "fa fa-share-square";
                            break;
                        case "Casa":
                            item.AlertDescription = Resource.AlertCasaDescDescription;
                            item.AlertName = Resource.PopupAlertCasaDesc;
                            item.IsDaily = false;
                            item.AlertUrl = Url.Action("AlertCurrentAccount", "CustomerAlert");
                            item.AlertCss = "fa fa-sort-amount-desc";
                            break;
                        case "Celebration":
                            item.AlertDescription = Resource.Company;
                            item.AlertName = Resource.Celebration;
                            item.IsDaily = true;
                            item.AlertUrl = Url.Action("Celebration", "CustomerAlert");
                            item.AlertCss = "fa fa-birthday-cake";
                            break;
                        case "BL":
                            item.AlertDescription = Resource.AlertBLMaturityDesc;
                            item.AlertName = Resource.PopupAlertBLMaturity;
                            item.IsDaily = true;
                            item.AlertUrl = Url.Action("AlertBLMaturity", "CustomerAlert");
                            item.AlertCss = "fa fa-bank";
                            break;
                        case "Limit":
                            item.AlertDescription = Resource.AlertLimitMaturityDesc;
                            item.AlertName = Resource.PopupAlertLimitMaturity;
                            item.IsDaily = true;
                            item.AlertUrl = Url.Action("AlertLimitMaturity", "CustomerAlert");
                            item.AlertCss = "fa fa-sign-in";
                            break;
                        case "Inactive":
                            item.AlertDescription = Resource.AlertCustInactiveDesc;
                            item.AlertName = Resource.PopupAlertCustInactive;
                            item.IsDaily = false;
                            item.AlertUrl = Url.Action("AlertCustInactive", "CustomerAlert");
                            item.AlertCss = "fa fa-frown-o";
                            break;
                        default:
                            break;
                    }
                }
            }
            return listRecords;
        }

        #endregion
    }
}
